Validate Data

Validate Field Value Immediately

Description
This customization will allow you to validate a field on a page immediately after the value is changed in the field. The page is validated the same way it would be if a button that causes validation were clicked. The Init method for the page is overridden to make the Custom Validator for Visible and Enabled. A new CustomValidator function is added to check the value and set the IsValid flag to False if the value entered by the user is invalid.
Variables
RecordControl
Select a record control
Validate Field Control
Select the field that you want to validate.
Applies to
RecordControl class
Code
 
/// 
/// This method sets the AutoPostBack property of ${Validate Field}'s TextBox to True such
/// that the TextChanged handler is called when the value is changed.
/// 
/// The object that raised the load event.
/// The object that contains the event data of the load event.
private void MyRecord_Init(object sender, System.EventArgs e) 
{ 
    if (!(this.Page.IsPostBack)) 
    { 
        this.${Validate Field Control}.AutoPostBack = true; 
    } 
    this.${Validate Field Control}.TextChanged += 
    new System.EventHandler(${Validate Field Control}_TextChanged);
} 
     
Applies to
RecordControl class
Code
 
/// 
/// This method validates the page whenever the text in the ${Validate Field} TextBox changes.
/// 
private void ${Validate Field Control}_TextChanged(object sender, System.EventArgs e) 
{ 
    try
    {
        Validate();
    }
    catch (Exception ex)
    {
        // Exception thrown by the Validate() sub is caught here.	
        // Report the error message to the user.
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "UNIQUE_SCRIPTKEY", "${Validate Field Control} cannot be empty ");
    }
}

/// 
/// Override the Validate() method. Add your validation logic here.
/// If the value of ${Validate Field Control} is not valid
/// then throw an exception. The exception is caught in the ${Validate Field Control}_TextChanged
/// event handler and it calls RegisterJScriptAlert to report the error message to the user.
/// Throwing an exception in Validate() method ensures that the record will not be saved. If you do not throw an exception
/// record will be saved. You can also validate other fields in the Validate() method.
/// In this customization ${Validate Field Control}_TextChanged will report an error message to the user
/// if an exception is thrown by the Validate method. After the ${Validate Field Control}_TextChanged event
/// when the user clicks on the save button, the exception thrown by the Validate() method will get caught
/// by the SaveButton_Click handler, which will call the RegisterJScriptAlert to report the error message to the user.
/// 
public override void Validate()
{
    // Call base Validate()
    base.Validate();

    // Add validation logic here
    if (this.${Validate Field Control}.Text == "")
    {
        throw new Exception();
    }    
}
  
Applies to
CSharpRecordControlConstructor class
Code
 
    // The following line will be inserted inside the
    // constructor for page class.
    this.Init += new System.EventHandler(MyRecord_Init);
     

Terms of Service Privacy Statement